home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / PHPUnit / TestResult.php < prev    next >
PHP Script  |  2004-10-01  |  7KB  |  301 lines

  1. <?php
  2. //
  3. // +------------------------------------------------------------------------+
  4. // | PEAR :: PHPUnit                                                        |
  5. // +------------------------------------------------------------------------+
  6. // | Copyright (c) 2002-2003 Sebastian Bergmann <sb@sebastian-bergmann.de>. |
  7. // +------------------------------------------------------------------------+
  8. // | This source file is subject to version 3.00 of the PHP License,        |
  9. // | that is available at http://www.php.net/license/3_0.txt.               |
  10. // | If you did not receive a copy of the PHP license and are unable to     |
  11. // | obtain it through the world-wide-web, please send a note to            |
  12. // | license@php.net so we can mail you a copy immediately.                 |
  13. // +------------------------------------------------------------------------+
  14. //
  15. // $Id: TestResult.php,v 1.10 2004/09/28 06:52:48 sebastian Exp $
  16. //
  17.  
  18. require_once 'PHPUnit/TestFailure.php';
  19. require_once 'PHPUnit/TestListener.php';
  20.  
  21. /**
  22.  * A TestResult collects the results of executing a test case.
  23.  *
  24.  * @author      Sebastian Bergmann <sb@sebastian-bergmann.de>
  25.  * @copyright   Copyright © 2002-2004 Sebastian Bergmann <sb@sebastian-bergmann.de>
  26.  * @license     http://www.php.net/license/3_0.txt The PHP License, Version 3.0
  27.  * @category    PHP
  28.  * @package     PHPUnit
  29.  */
  30. class PHPUnit_TestResult {
  31.     /**
  32.     * @var    array
  33.     * @access protected
  34.     */
  35.     var $_errors = array();
  36.  
  37.     /**
  38.     * @var    array
  39.     * @access protected
  40.     */
  41.     var $_failures = array();
  42.  
  43.     /**
  44.     * @var    array
  45.     * @access protected
  46.     */
  47.     var $_listeners = array();
  48.  
  49.     /**
  50.     * @var    array
  51.     * @access protected
  52.     */
  53.     var $_passedTests = array();
  54.  
  55.     /**
  56.     * @var    integer
  57.     * @access protected
  58.     */
  59.     var $_runTests = 0;
  60.  
  61.     /**
  62.     * @var    boolean
  63.     * @access private
  64.     */
  65.     var $_stop = FALSE;
  66.  
  67.     /**
  68.     * Adds an error to the list of errors.
  69.     * The passed in exception caused the error.
  70.     *
  71.     * @param  object
  72.     * @param  object
  73.     * @access public
  74.     */
  75.     function addError(&$test, &$t) {
  76.         $this->_errors[] = new PHPUnit_TestFailure($test, $t);
  77.  
  78.         for ($i = 0; $i < sizeof($this->_listeners); $i++) {
  79.             $this->_listeners[$i]->addError($test, $t);
  80.         }
  81.     }
  82.  
  83.     /**
  84.     * Adds a failure to the list of failures.
  85.     * The passed in exception caused the failure.
  86.     *
  87.     * @param  object
  88.     * @param  object
  89.     * @access public
  90.     */
  91.     function addFailure(&$test, &$t) {
  92.         $this->_failures[] = new PHPUnit_TestFailure($test, $t);
  93.  
  94.         for ($i = 0; $i < sizeof($this->_listeners); $i++) {
  95.             $this->_listeners[$i]->addFailure($test, $t);
  96.         }
  97.     }
  98.  
  99.     /**
  100.     * Registers a TestListener.
  101.     *
  102.     * @param  object
  103.     * @access public
  104.     */
  105.     function addListener(&$listener) {
  106.         if (is_object($listener) &&
  107.             is_a($listener, 'PHPUnit_TestListener')) {
  108.             $this->_listeners[] = $listener;
  109.         }
  110.     }
  111.  
  112.     /**
  113.     * Adds a passed test to the list of passed tests.
  114.     *
  115.     * @param  object
  116.     * @access public
  117.     */
  118.     function addPassedTest(&$test) {
  119.         $this->_passedTests[] = $test;
  120.     }
  121.  
  122.     /**
  123.     * Informs the result that a test was completed.
  124.     *
  125.     * @param  object
  126.     * @access public
  127.     */
  128.     function endTest(&$test) {
  129.         for ($i = 0; $i < sizeof($this->_listeners); $i++) {
  130.             $this->_listeners[$i]->endTest($test);
  131.         }
  132.     }
  133.  
  134.     /**
  135.     * Gets the number of detected errors.
  136.     *
  137.     * @return integer
  138.     * @access public
  139.     */
  140.     function errorCount() {
  141.         return sizeof($this->_errors);
  142.     }
  143.  
  144.     /**
  145.     * Returns an Enumeration for the errors.
  146.     *
  147.     * @return array
  148.     * @access public
  149.     */
  150.     function &errors() {
  151.         return $this->_errors;
  152.     }
  153.  
  154.     /**
  155.     * Gets the number of detected failures.
  156.     *
  157.     * @return integer
  158.     * @access public
  159.     */
  160.     function failureCount() {
  161.         return sizeof($this->_failures);
  162.     }
  163.  
  164.     /**
  165.     * Returns an Enumeration for the failures.
  166.     *
  167.     * @return array
  168.     * @access public
  169.     */
  170.     function &failures() {
  171.         return $this->_failures;
  172.     }
  173.  
  174.     /**
  175.     * Returns an Enumeration for the passed tests.
  176.     *
  177.     * @return array
  178.     * @access public
  179.     */
  180.     function &passedTests() {
  181.         return $this->_passedTests;
  182.     }
  183.  
  184.     /**
  185.     * Unregisters a TestListener.
  186.     * This requires the Zend Engine 2 (to work properly).
  187.     *
  188.     * @param  object
  189.     * @access public
  190.     */
  191.     function removeListener(&$listener) {
  192.         for ($i = 0; $i < sizeof($this->_listeners); $i++) {
  193.             if ($this->_listeners[$i] === $listener) {
  194.                 unset($this->_listeners[$i]);
  195.             }
  196.         }
  197.     }
  198.  
  199.     /**
  200.     * Runs a TestCase.
  201.     *
  202.     * @param  object
  203.     * @access public
  204.     */
  205.     function run(&$test) {
  206.         $this->startTest($test);
  207.         $this->_runTests++;
  208.         $test->runBare();
  209.         $this->endTest($test);
  210.     }
  211.  
  212.     /**
  213.     * Gets the number of run tests.
  214.     *
  215.     * @return integer
  216.     * @access public
  217.     */
  218.     function runCount() {
  219.         return $this->_runTests;
  220.     }
  221.  
  222.     /**
  223.     * Checks whether the test run should stop.
  224.     *
  225.     * @access public
  226.     */
  227.     function shouldStop() {
  228.         return $this->_stop;
  229.     }
  230.  
  231.     /**
  232.     * Informs the result that a test will be started.
  233.     *
  234.     * @param  object
  235.     * @access public
  236.     */
  237.     function startTest(&$test) {
  238.         for ($i = 0; $i < sizeof($this->_listeners); $i++) {
  239.             $this->_listeners[$i]->startTest($test);
  240.         }
  241.     }
  242.  
  243.     /**
  244.     * Marks that the test run should stop.
  245.     *
  246.     * @access public
  247.     */
  248.     function stop() {
  249.         $this->_stop = TRUE;
  250.     }
  251.  
  252.     /**
  253.     * Returns a HTML representation of the test result.
  254.     *
  255.     * @return string
  256.     * @access public
  257.     */
  258.     function toHTML() {
  259.         return '<pre>' . htmlspecialchars($this->toString()) . '</pre>';
  260.     }
  261.  
  262.     /**
  263.     * Returns a text representation of the test result.
  264.     *
  265.     * @return string
  266.     * @access public
  267.     */
  268.     function toString() {
  269.         $result = '';
  270.  
  271.         foreach ($this->_passedTests as $passedTest) {
  272.             $result .= sprintf(
  273.               "TestCase %s->%s() passed\n",
  274.  
  275.               get_class($passedTest),
  276.               $passedTest->getName()
  277.             );
  278.         }
  279.  
  280.         foreach ($this->_failures as $failedTest) {
  281.             $result .= $failedTest->toString();
  282.         }
  283.  
  284.         return $result;
  285.     }
  286.     /**
  287.     * Returns whether the entire test was successful or not.
  288.     *
  289.     * @return boolean
  290.     * @access public
  291.     */
  292.     function wasSuccessful() {
  293.         if (empty($this->_errors) && empty($this->_failures)) {
  294.             return TRUE;
  295.         } else {
  296.             return FALSE;
  297.         }
  298.     }
  299. }
  300. ?>
  301.